//
//  KindViewController.m
//  FNMenu
//
//  Created by 航汇聚科技 on 2018/9/6.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "KindViewController.h"
#import "NSDictionary+Safety.h"
#import "NSArray+Safety.h"
#import "KindHeaderCollectionReusableView.h"
#import "KindCollectionViewCell.h"
#import "ListViewController.h"
#import "NetManager.h"
@interface KindViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *kinds;

@end

@implementation KindViewController

#pragma mark - lifeCycle
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NetManager defaultNetManager] fetchNetDataWithURLStr:@"home/menu/classify" params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.kinds = [responseObject safeObjectForKey:@"result"];
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.kinds.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [[[self.kinds safeObjectAtIndex:section] safeObjectForKey:@"list"] count];
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    KindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kindCell" forIndexPath:indexPath];
    cell.kindInfo = [[[self.kinds safeObjectAtIndex:indexPath.section] safeObjectForKey:@"list"] safeObjectAtIndex:indexPath.item];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    KindHeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"kindHeader" forIndexPath:indexPath];
    header.parentKindInfo = [self.kinds safeObjectAtIndex:indexPath.section];
    return header;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    NSString *text = [[[[self.kinds safeObjectAtIndex:indexPath.section] safeObjectForKey:@"list"] objectAtIndex:indexPath.item] safeObjectForKey:@"name"];
    CGFloat extraWidth = 14;
    if (text.length == 1) {
        extraWidth = extraWidth * 2.6;
    }
    if (text.length == 2) {
        extraWidth = extraWidth * 1.6;
    }
    UIFont *font = [UIFont fontWithName:@"AppleSDGothicNeo-Regular" size:20.0];
    CGSize size = CGSizeMake(MAXFLOAT, 30.0f);
    CGSize buttonSize = [text boundingRectWithSize:size
                                              options:NSStringDrawingTruncatesLastVisibleLine  | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                           attributes:@{NSFontAttributeName:font}
                                              context:nil].size;
    return CGSizeMake(buttonSize.width + extraWidth, buttonSize.height + 14);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    KindCollectionViewCell *cell = (KindCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    ListViewController *listVC = [ListViewController listVCFromStroyBoard];
    listVC.cID = (cell.kindBtn.tag - 1000);
    listVC.title = [[[[self.kinds safeObjectAtIndex:indexPath.section] safeObjectForKey:@"list"] safeObjectAtIndex:indexPath.item] safeObjectForKey:@"name"];
    [self.navigationController pushViewController:listVC animated:YES];
}
#pragma mark - public
+ (instancetype)kindViewControllerFromStoryboard {
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"KindVC"];;
}
@end
